/*
 * @name: 验证码
 * @Author: along
 * @Edition: 1.3.5
 * @Date: 2020-04-22
 */
<template>
    <div class="container appPhoneCode">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box"/>
            </div>
            <div class="header-left">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click=" dialogVisible = true "
                    >
                        新增定向验证码
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="appPhoneCode"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="phone"
                label="手机号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="student_info.true_name"
                label="用户姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="phone_verify_code"
                label="验证码"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="admin_info.username"
                label="创建人"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="create_time"
                label="创建时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteCode(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->

        <!-- 新增/编辑弹窗 -->
        <el-dialog
            :visible.sync="dialogVisible"
            width="480px"
            :close-on-click-modal="false"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="手机号"
                    prop="phone"
                >
                    <el-input
                        v-model="ruleForm.phone"
                        size="small"
                        type="text"
                        maxlength="11"
                        placeholder="请输入你的手机号"
                        show-word-limit
                    />
                </el-form-item>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="dialogVisible = false"
                    size="small"
                    class="dialog-mr"
                >
                    取 消
                </el-button>
                <el-button
                    type="primary"
                    @click="submitForm('ruleForm')"
                    size="small"
                >
                    创 建
                </el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    /**
     * @param   {String}     type                           打开弹窗类型，add:新建 edit:编辑
     * @param   {Object}     rules                          弹窗字段验证规则
     * @param   {Object}     ruleForm                       弹窗字段
     * @param   {Boolean}    dialogVisible                  弹窗开关
     * @param   {Object}     page                           分页
     * @param   {Array}      tableData                      数据列表
     * @param   {OBject}     authResult                     权限列表
     */
    mixins: [back],
    data () {
        return {
            type: '',
            rules: {
                phone: [
                    { required: true, message: '请填写手机号,不可以为空', trigger: 'change' }
                ],
                code: [
                    { required: true, message: '请填写验证码,不可以为空', trigger: 'change' }
                ]
            },
            ruleForm: {
                phone: '',
                // code: ''
            },
            dialogVisible: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {
        dialogVisible (val) {
            if(!val) {
                this.ruleForm = {
                    phone: '',
                    // code: ''
                };
                this.$refs['ruleForm'].resetFields();
            }
        }
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '验证码'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/student_user_temp_login_code%5Cget_list', {}, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    // this.page.now_page = resp.data.page.now_page;
                    // this.page.total_count = resp.data.page.total_count;
                    // this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 创建/编辑提交弹窗数据
         */
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // const numberLint = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;

                    // if(!(/^1[3456789]\d{9}$/.test(this.ruleForm.phone))){
                    //     this.$message({
                    //         type: 'waring',
                    //         message: '请输入正确的手机号码',
                    //         center: true
                    //     });
                    //     return;
                    // }

                    if(this.ruleForm.phone.length == 11) {
                        this.$post('/student/student_user_temp_login_code%5Cadd',{
                            phone: this.ruleForm.phone
                        },resp => {
                            if(resp.code == 1) {
                                this.getTableData();
                                this.dialogVisible = !this.dialogVisible;
                                this.$message({
                                    type: 'success',
                                    message: '新增成功',
                                    center: true
                                });
                            } else {
                                this.$message({
                                    type: 'waring',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: '请输入正确的手机号码',
                            center: true
                        });
                    }

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 删除验证码
         */
        fnDeleteCode (row) {
            if(this.authResult.auth_delete) {
                this.$confirm('是否要删除该条验证码', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$post('/student/student_user_temp_login_code%5Cdelete',{
                        id: row.id
                    },resp => {
                        if(resp.code == 1) {
                            this.getTableData();
                            this.$message({
                                type: 'success',
                                message: '删除成功',
                                center: true
                            });
                        } else {
                            this.$message({
                                type: 'waring',
                                message: resp.msg,
                                center: true
                            });
                        }
                    });
                });
            } else {
                this.$message({
                    type: 'waring',
                    message: '只有超级管理员可以删除',
                    center: true
                });
            }
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .dialog-footer {
        display: flex;
        width: 100%;
        justify-content: center;
        .dialog-mr {
            margin-right: 100px;
        }
    }
</style>
<style lang="less">
    .appPhoneCode{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
    }
</style>